package com.ts.api.module.api.convert.market.convert;

import com.gitee.apanlh.util.base.Eq;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarket;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarketConvertType;
import com.ts.api.module.api.convert.core.context.ApiConvertRegister;
import com.ts.api.module.api.convert.market.handler.ChannelMarketConvertHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *  渠道产品(贷超)上下文-买量
 *
 *  @author Pan
 */
@Component
public class ChannelMarketConvert extends ApiConvertRegister<ChannelMarketConvertHandler, ApiChannelMarket> {

    /**
     *  默认构造函数
     */
    public ChannelMarketConvert() {
        super(
            ChannelMarketConvertHandler.class,
            ApiChannelMarket.class
        );
    }

    @Override
    public void registerHandler(ApplicationContext applicationContext, Map<String, ChannelMarketConvertHandler> contextCache) {
        ApiChannelMarketConvertType[] values = ApiChannelMarketConvertType.values();
        //  枚举方式
        for (ApiChannelMarketConvertType apiChannelMarketConvertType : values) {
            if (Eq.enums(ApiChannelMarketConvertType.NULL, apiChannelMarketConvertType)) {
                continue;
            }

            String prefixBeanName = apiChannelMarketConvertType.getPrefixBeanName();
            classRegister(prefixBeanName, (annotation) -> Eq.enums(annotation.type(), apiChannelMarketConvertType));
            factoryRegister(prefixBeanName, (annotation) -> Eq.enums(annotation.type(), apiChannelMarketConvertType));
        }
    }

    @Override
    public String extractKey(ApiChannelMarket annotation) {
        return annotation.uniqueCode();
    }

    /**
     *  根据枚举获取处理器
     *
     *  @param  apiChannelMarketConvertType       渠道贷超枚举
     *  @return ConsumerMarketConvertHandler
     */
    public ChannelMarketConvertHandler getHandler(String channelUnicode, ApiChannelMarketConvertType apiChannelMarketConvertType) {
        return super.getHandler(apiChannelMarketConvertType.getPrefixBeanName() + channelUnicode);
    }

    /**
     *  根据枚举获取处理器
     *
     *  @param  apiChannelMarketConvertType       渠道贷超枚举
     *  @return ConsumerMarketConvertHandler
     */
    public ChannelMarketConvertHandler getDefaultHandler(ApiChannelMarketConvertType apiChannelMarketConvertType) {
        return super.getHandler(apiChannelMarketConvertType.getPrefixBeanName() + apiChannelMarketConvertType.getDefaultBeanName());
    }
}

